import Vue from 'vue'
import Vuex from 'vuex'
// import mutations from './mutations'
import actions from './actions'
// import getters from './getters'

//安装插件
Vue.use(Vuex);
const state = {
    isLogin: 0, //登录状态
    allUsers: [], //全部用户
    user: {}, //用户信息
    token: localStorage.getItem('token') ? localStorage.getItem('token') : '', //用户token
    publicMsgList: [], //存放群聊消息
    shopCart: [], //购物车数据
    detailComment: {} //话题详情
};
//创建store对象
const store = new Vuex.Store({
    state,
    mutations: {
        //给state设置token值
        setToken(state, token) {
            state.token = token;
            localStorage.setItem('token', token)
        },
        //删除token值
        delToken(state) {
            state.token = '';
            localStorage.removeItem('token')
        },
        //改变登录状态
        changeStatus(state, status) {
            state.isLogin = status
        },
        //保存登录用户信息
        saveUser(state, payload) {
            state.user = payload
            localStorage.setItem('userInfo', JSON.stringify(payload))
        },
        //删除用户信息
        delUser(state) {
            state.user = {}
            localStorage.removeItem('userInfo')
        },
        //用户退出登录
        loginOut(state) {
            state.user = {}
            state.isLogin = 0
            state.token = '';
            state.shopCart = ''
            localStorage.removeItem('userInfo')
            localStorage.removeItem('token')
            localStorage.removeItem('shopCart')
        },
        //存放群聊消息
        newPublicMsg(state, msg) {
            state.publicMsgList.push(msg)
        },
        //存放私聊消息
        newPrivateUs(state, msg) {
            // console.log('监听到新私聊消息(自己发的)', );
            state[`${msg.receive_id}`].push(msg)
        },
        newPrivateOther(state, msg) {
            // console.log('监听到新私聊消息(别人发的)')    
            state[`${msg.send_id}`].push(msg);
            state.allUsers.forEach(e => {
                if (e._id == msg.send_id) {
                    console.log('发送者', e)
                    e.newmsg = 1
                }
            })
        },
        //创建私聊数组并且保存所有用户
        createArr(state, users) {
            console.log('mutation接收到用户', users)
            for (let i = 0; i < users.length; i++) {
                state[`${users[i]._id}`] = []
                users[i].newmsg = 0;
                // console.log(state[`${users[i]._id}`])
            }
            state.allUsers = users
        },
        //新插入一个新注册的用户的聊天记录
        createNewArr(state, sendId) {
            state[`${sendId}`] = []
        },
        //获取群聊记录
        getAllMsg(state, data) {
            state.publicMsgList = data
        },
        //获取好友聊天记录
        getPrivateMsg(state, data) {
            let receive_id = data.receive_id
            state[`${receive_id}`] = []
            state[`${receive_id}`].push(...data.msgList)
        },
        //清除未读状态
        read(state, uid) {
            state.allUsers.forEach(e => {
                if (e._id == uid) {
                    e.newmsg = 0
                }
            })
        },
        //添加到购物车
        addToCart(state, data) {
            state.shopCart.push(data)
            console.log(state.shopCart)
            localStorage.setItem('shopCart', JSON.stringify(state.shopCart))
                // alert('添加购物车成功')
        },
        //同步购物车数据
        initShopCart(state, data) {
            state.shopCart = data
            console.log('同步购物车数据成功', state.shopCart)
        },
        check(state, index) { //修改商品选中状态
            state.shopCart[index].isCheck = !state.shopCart[index].isCheck
            console.log('修改选中状态成功')
        },
        selectAll(state) { //全选
            state.shopCart.forEach(e => e.isCheck = true)
            console.log('全选')
        },
        selectNone(state) {
            state.shopCart.forEach(e => e.isCheck = false)
            console.log('全不选')
        },
        delete(state, index) { //删除商品
            state.shopCart.splice(index, 1)
            localStorage.setItem('shopCart', JSON.stringify(state.shopCart));
            // alert('删除商品成功')
        },
        detailComment(state, data) { //改变查看的话题
            state.detailComment = data
        },


    },
    actions: {
        SetToken({ commit }, token) { //设置token值
            commit('setToken', token)
        },
        ChangeStatus({ commit }) { //改变登录状态
            commit('changeStatus', 1)
        },
        SaveUserInfo({ commit }, userInfo) { //保存用户信息
            commit('saveUser', userInfo)
        },
        DelUserInfo({ commit }) { //删除用户信息
            commit('delUser')
        },
        LoginOut({ commit }) { //用户退出登录
            commit('loginOut')
        },
        NewPublicMsg({ commit }, msg) { //给publicMsgList插入新消息
            commit('newPublicMsg', msg)
        },
        NewPrivateUs({ commit }, msg) { //存放私聊信息自己发的
            commit('newPrivateUs', msg)
        },
        NewPrivateOther({ commit }, msg) { //存放私聊信息别人发的
            commit('newPrivateOther', msg)
        },
        CreateArr({ commit }, users) { //动态创建私聊存放数组
            console.log('接收到全部用户', users)
            commit('createArr', users)
        },
        CreateNewArr({ commit }, sendId) { //动态创新新注册用户的消息存放数组
            commit('createNewArr', sendId)
        },
        GetAllMsg({ commit }, data) { //获取群聊记录
            console.log('接收到获取群聊消息的请求')
            commit('getAllMsg', data)
        },
        GetPrivateMsg({ commit }, data) { //获取与好友的聊天记录
            console.log('获取好友' + data.receive_id + '的聊天记录')
            commit('getPrivateMsg', data)
        },
        Read({ commit }, uid) { //清除未读状态
            commit('read', uid)
        },
        AddToCart({ commit }, data) { //添加到购物车
            console.log(data)
            commit('addToCart', data)
        },
        InitShopCart({ commit }, data) { //同步购物车数据
            commit('initShopCart', data)
        },
        Check({ commit }, index) { //修改选中和不选中状态
            commit('check', index)
        },
        SelectAll({ commit }) { //全选
            commit('selectAll')
        },
        SelectNone({ commit }) { //全不选
            commit('selectNone')
        },
        Delete({ commit }, index) { //删除
            commit('delete', index)
        },
        DetailComment({ commit }, data) { //改变查看的话题
            commit('detailComment', data)
        }
    },
    getters: {
        user(state) { //用户信息
            return state.user
        },
        msgList(state) { //群聊信息
            return state.publicMsgList
        },
        allUsers(state) { //所有用户
            return state.allUsers
        },
        isLogin(state) { //是否登录
            return state.isLogin
        },
        shopCart(state) { //购物车
            return state.shopCart
        },
        detailComment(state) { //查看的话题
            return state.detailComment
        }

    }
});
export default store;